package de.tudarmstadt.gdi1.project.analyse;

import de.tudarmstadt.gdi1.project.alphabet.Alphabet;
import de.tudarmstadt.gdi1.project.alphabet.Dictionary;
import de.tudarmstadt.gdi1.project.alphabet.Distribution;
import de.tudarmstadt.gdi1.project.analysis.EncryptionOracle;
import de.tudarmstadt.gdi1.project.analysis.monoalphabetic.MonoalphabeticCpaNpaCryptanalysis;
import de.tudarmstadt.gdi1.project.cipher.substitution.monoalphabetic.MonoalphabeticCipher;

public class ChoosenAndKnownPlaintext implements MonoalphabeticCpaNpaCryptanalysis{

	@Override
	public Object knownPlaintextAttack(String ciphertext, String plaintext,
			Distribution distribution) {
		Object a = knownPlaintextAttack(ciphertext, plaintext, distribution.getAlphabet());
		return a;
	}

	@Override
	public Object knownPlaintextAttack(String ciphertext, String plaintext,
			Distribution distribution, Dictionary dictionary) {
		Object a = knownPlaintextAttack(ciphertext, plaintext, distribution.getAlphabet());
		return a;
	}

	@Override
	public char[] knownPlaintextAttack(String ciphertext, String plaintext,
			Alphabet alphabet) {
		char[] a = new char[alphabet.size()];
		for (int i = 0; i < a.length; i++){
			a[i] = ' ';
		}
		for(int i=0; i < plaintext.length(); i++){
			a[alphabet.getIndex(plaintext.charAt(i))] = ciphertext.charAt(i);					
		}
		return a;
	}

	@Override
	public char[] chosenPlaintextAttack(
			EncryptionOracle<MonoalphabeticCipher> oracle, Alphabet alphabet) {
		return oracle.encrypt(alphabetToString(alphabet)).toCharArray();
	}
	
	String alphabetToString(Alphabet alphabet){
		StringBuilder b = new StringBuilder();
		for (char a : alphabet){
			b.append(a);
		}
		return b.toString();
	}

}
